﻿function BuildHeader()
{
	if (_page == "rule_list")
		return BuildAjaxPath(true) + "?Lang=" + _lang + "&IsRecycled=" + $("#IsRecycled").val() + "&Sort=" + $("#Sort").val() + "&Keyword=" + encodeURIComponent($("#Keyword").val()) + "&KeywordType=" + $("#KeywordType").val() + "&Page=" + _pageNumber;
	else if (_page == "result_list")
		return BuildAjaxPath(true) + "?Lang=" + _lang + "&ID=" + _id + "&Sort=" + $("#Sort").val() + "&Keyword=" + encodeURIComponent($("#Keyword").val()) + ($("#KeywordType").val() == null ? "" : "&KeywordType=" + $("#KeywordType").val()) + "&Page=" + _pageNumber;
	else
	    return BuildAjaxPath();
}

function Search()
{
	if (arguments.length > 0 && typeof(arguments[0]) != "object")
		_pageNumber = arguments[0];
	
	SetStatusMessage(_lang_working);

	$.ajax(
	{
		url : BuildHeader(),
		data : {},
		type : "POST",
		error : function(request)
		{
			Alert(_lang_operation_fail);
			SetStatusMessage();
		},
		success : function(data)
		{
			var list = $("#List");

			list.html(HandleFake404(data, "text"));
			SetStatusMessage();

			if (typeof(SetupIme) == "function")
				SetupIme();
				
			if (_page == "rule_list")
			{
				MakeButton();
			}
		}
	});
}

function MakeButton()
{
	var buttons = "";

	if (_page == "rule_list")
	{
	    //回收
	    var obj = $("#IsRecycled")[0];
	    if (obj.selectedIndex == 1)
	    {
		    buttons += "<button class=\"Button\" onclick=\"UpdateRule('Recycle')\" >" + _lang_recycle + "</button>";
	    }
	    else if (obj.selectedIndex == 2)
	    {
		    buttons += "<button class=\"Button\" onclick=\"UpdateRule('Delete')\" >" + _lang_delete + "</button>";
		    buttons += "<button class=\"Button\" onclick=\"UpdateRule('Recover')\" >" + _lang_recover + "</button>";
	    }
	}	

	$("#ExtraButton").html(buttons);
}

//-------------------------------------------------
//Start : 规则管理
function AddRule()
{
	if (this.Name.value == "")
	{
		Alert(_lang_name_empty, function(){this.Name.select();}, this);
		return false;
	}
	
	if (this.Every.value == "Week" && $("#Day :checked").length == 0)
	{
		Alert(_lang_day_incorrect);
		return false;
	}

	var data = "Name=" + encodeURIComponent(this.Name.value);
	if ($("#GuidCheckBox")[0].checked)
		data += "&ID=" + this.ID.value;
	data += "&Config=" + encodeURIComponent(this.Config.value);
	data += "&Description=" + encodeURIComponent(this.Description.value);
	data += "&AutoExport=" + $("#AutoExportYes")[0].checked;
	data += "&UserAgent=" + encodeURIComponent(this.UserAgent.value);

	data += "&Every=" + encodeURIComponent(this.Every.value);
	if (this.Every.value == "Week")
	{
		$("#Day :checked").each(function()
		{
			data += "," + this.value;
		});
	}

	if (this.Every.value != "")
		data += "&StartTime=" + FormatDate(new Date($("[name=Year]").val(), $("[name=Month]").val(), $("[name=Date]").val(), $("[name=Hour]").val(), $("[name=Minute]").val()), "YYYY-MM-DD hh:mm:ss");

	$(":submit")[0].disabled = true;
	SetStatusMessage(_lang_working);
	
	$.ajax(
	{
		url : _webPath + "alicms.aspx?Lang=" + _lang + "&Object=Rule&Operation=Add",
		data : data,
		type : "POST",
		error : function(request)
		{
			Alert(_lang_operation_fail);
			SetStatusMessage();
			$(":submit")[0].disabled = false;
		},
		success : function(data)
		{
			Alert(_lang_operation_success);
			SetStatusMessage();
			$(":submit")[0].disabled = false;
		}
	});
	
	return false;
}

function EditRule()
{
	if (this.Name.value == "")
	{
		Alert(_lang_name_empty, function(){this.Name.select();}, this);
		return false;
	}
	
	if (this.Every.value == "Week" && $("#Day :checked").length == 0)
	{
		Alert(_lang_day_incorrect);
		return false;
	}

	var data = "Name=" + encodeURIComponent(this.Name.value);
	if ($("#GuidCheckBox")[0].checked)
		data += "&ID=" + this.ID.value;
	data += "&Config=" + encodeURIComponent(this.Config.value);
	data += "&Description=" + encodeURIComponent(this.Description.value);
	data += "&AutoExport=" + $("#AutoExportYes")[0].checked;
	data += "&UserAgent=" + encodeURIComponent(this.UserAgent.value);

	data += "&Every=" + encodeURIComponent(this.Every.value);
	if (this.Every.value == "Week")
	{
		$("#Day :checked").each(function()
		{
			data += "," + this.value;
		});
	}

	if (this.Every.value != "")
		data += "&StartTime=" + FormatDate(new Date($("[name=Year]").val(), $("[name=Month]").val(), $("[name=Date]").val(), $("[name=Hour]").val(), $("[name=Minute]").val()), "YYYY-MM-DD hh:mm:ss");
	else
		data += "&StartTime=";
	data += "&EndTime=" + encodeURIComponent(this.EndTime.value);
	data += "&LastID=" + encodeURIComponent(this.LastID.value);

	$(":submit")[0].disabled = true;
	SetStatusMessage(_lang_working);
	
	$.ajax(
	{
		url : _webPath + "alicms.aspx?Lang=" + _lang + "&Object=Rule&Operation=Modify&ID=" + _id,
		data : data,
		type : "POST",
		error : function(request)
		{
			Alert(_lang_operation_fail);
			SetStatusMessage();
			$(":submit")[0].disabled = false;
		},
		success : function(data)
		{
			Alert(_lang_operation_success);
			SetStatusMessage();
			$(":submit")[0].disabled = false;
		}
	});
	
	return false;	
}

function UpdateRule(action, id)
{
	if (id == null)
	{
		id = "";
		$("[name=Sel]:checked").each(function(){id += id == "" ? this.value : "|" + this.value;});
		if(id == "")
		{
			Alert(_lang_selection_empty);
			return;
		}
		
		if(action == "Delete")
			Confirm(_lang_confirm_delete, function(returnValue){if (returnValue)UpdateRule(action, id);}, this);
		else
			UpdateRule(action, id);
	}
	else
	{
		SetStatusMessage(_lang_working);

		$.ajax(
		{
			url : _webPath + "alicms.aspx?Lang=" + _lang + "&Object=Rule&Operation=" + action + "&ID=" + id + "&Transfer=" + encodeURIComponent(BuildHeader()),
			data : {},
			type : "POST",
			error : function(request)
			{
				Alert(_lang_operation_fail);
				SetStatusMessage();
			},
			success : function(data)
			{
				Alert(_lang_operation_success);
				SetStatusMessage();

				$("#List").html(HandleFake404(data, "text"));	
			}
		});
	}
}
//End : 规则管理
//-------------------------------------------------

//-------------------------------------------------
//Start : 规则结果
function UpdateResult(action, id)
{
	if (id == null)
	{
		id = "";
		$("[name=Sel]:checked").each(function(){id += id == "" ? this.value : "|" + this.value;});
		if(id == "")
		{
			Alert(_lang_selection_empty);
			return;
		}
		
		if(action == "Delete")
			Confirm(_lang_confirm_delete, function(returnValue){if (returnValue)UpdateResult(action, id);}, this);
		else
			UpdateResult(action, id);
	}
	else
	{
		SetStatusMessage(_lang_working);

		$.ajax(
		{
			url : _webPath + "alicms.aspx?Lang=" + _lang + "&Object=Rule.Result&Operation=" + action + "&RuleID=" + _id + "&ID=" + id + "&Transfer=" + encodeURIComponent(BuildHeader()),
			data : {},
			type : "POST",
			error : function(request)
			{
				Alert(_lang_operation_fail);
				SetStatusMessage();
			},
			success : function(data)
			{
				Alert(_lang_operation_success);
				SetStatusMessage();

				$("#List").html(HandleFake404(data, "text"));	
			}
		});
	}
}

function StartExecute()
{
	var me = this;
	me.disabled = true;
	
	SetStatusMessage(_lang_start_working);

	$.ajax(
	{
		url : _webPath + "alicms.aspx?Lang=" + _lang + "&Object=Rule&Operation=StartExecute&ID=" + _id,
		data : {},
		type : "POST",
		error : function(request)
		{
			Alert(_lang_operation_fail);
			SetStatusMessage();
			me.disabled = false;
		},
		success : function(data)
		{
			setTimeout(RefreshResult, _interval);
		}
	});
}

function StopExecute()
{
	SetStatusMessage(_lang_working);

	$.ajax(
	{
		url : _webPath + "alicms.aspx?Lang=" + _lang + "&Object=Rule&Operation=StopExecute&ID=" + _id,
		data : {},
		type : "POST",
		error : function(request)
		{
			Alert(_lang_operation_fail);
			SetStatusMessage();
			$("#StartExecute")[0].disabled = false;
		},
		success : function(data)
		{
			SetStatusMessage();
			$("#StartExecute")[0].disabled = false;
		}
	});
}

function RefreshResult()
{
	$.ajax(
	{
		url : BuildHeader(),
		data : {},
		type : "POST",
		error : function(request)
		{
			Alert(_lang_operation_fail);
		},
		success : function(data)
		{
			var list = $("#List");
			list.html(HandleFake404(data, "text"));
			
			$.ajax(
			{
				url : _webPath + "alicms.aspx?Lang=" + _lang + "&Object=Rule&Operation=IsRunning&ID=" + _id,
				data : {},
				type : "POST",
				error : function(request)
				{
					Alert(_lang_operation_fail);
				},
				success : function(data)
				{
					if (data.toLowerCase() == "true")
					{
						setTimeout(RefreshResult, _interval);
					}
					else
					{
						SetStatusMessage();
						$("#StartExecute")[0].disabled = false;
					}
				}
			});
		}
	});
}

function Export()
{
	var me = this;
	me.disabled = true;
	
	SetStatusMessage(_lang_working);

	$.ajax(
	{
		url : _webPath + "alicms.aspx?Lang=" + _lang + "&Object=Rule&Operation=Export&ID=" + _id,
		data : {},
		type : "POST",
		error : function(request)
		{
			Alert(_lang_operation_fail);
			SetStatusMessage();
			me.disabled = false;
		},
		success : function(data)
		{
			Alert(_lang_operation_success);
			SetStatusMessage();
			me.disabled = false;
		}
	});
}

function ReplaceResult()
{
	if (this.From.type == "text" && this.From.value == "")
	{
		Alert(_lang_from_empty, function(){this.From.select();}, this);
		return false;
	}
	
	var data = "Property=" + this.Property.value;
	data += "&From=" + encodeURIComponent(this.From.value);
	data += "&To=" + encodeURIComponent(this.To.value);
	
	$(":submit").eq(0).attr("disabled", true);
	SetStatusMessage(_lang_working);

	$.ajax(
	{
		url : _webPath + "alicms.aspx?Lang=" + _lang + "&Object=Rule.Result&Operation=Replace&RuleID=" + _id,
		data : data,
		type : "POST",
		error : function(request)
		{
			Alert(_lang_operation_fail);
			SetStatusMessage();
        	$(":submit").eq(0).attr("disabled", false);
		},
		success : function(data)
		{
		    Alert(_lang_operation_success);
		    SetStatusMessage();
		    $(":submit").eq(0).attr("disabled", false);
		}
	});
	
	return false;
}
//End : 规则结果
//-------------------------------------------------

//-------------------------------------------------
//Start : 规则导出
function Module_OnChange()
{
	SetStatusMessage(_lang_working);

	$.ajax(
	{
		url : "ajax_instance.aspx?ModuleID=" + $("[name=Module]").val(),
		data : {},
		type : "GET",
		error : function(request)
		{
			Alert(_lang_operation_fail);
			SetStatusMessage();
		},
		success : function(data)
		{
			SetStatusMessage();
				
			var items = eval(data);
			if (items.length == 0)
				return;

			var html = "";
			for (var i = 0; i < items.length; i++)
			{
				html += "<option value='" + items[i].ID + "'>" + items[i].Name + "</option>";
			}

			var instance = $("[name=Instance]");
			instance[0].length = 1;
			instance.append(html);
				
			if (_export != null)
				instance.val(_export.documentElement.getAttribute("Instance"));
		}
	});

	$.ajax(
	{
		url : "ajax_object.aspx?ModuleID=" + $("[name=Module]").val(),
		data : {},
		type : "GET",
		error : function(request)
		{
			Alert(_lang_operation_fail);
			SetStatusMessage();
		},
		success : function(data)
		{
			var items = eval(data);
			if (items.length == 0)
				return;

			var html = "";
			for (var i = 0; i < items.length; i++)
			{
				var name = items[i].Name;
				
				var parentID = items[i].ParentID;
				while (parentID != g_empty)
				{
					for (var j = 0; j < items.length; j++)
					{
						if (parentID == items[j].ID)
						{
							name = items[j].Name + "." + name;
							parentID = $.trim(items[j].ParentID);
							break;
						}
					}
				}
				
				html += "<option value='" + items[i].InnerID + "'>" + name + "</option>";
			}

			var object = $("[name=Object]");
			object[0].length = 1;
			object.append(html);
				
			if (_export != null)
			{
				var value = _export.documentElement.getAttribute("Object");
				object.children().each(function()
				{
					if ($(this).text() == value)
					{
						$(this).attr("selected", true);
						return;
					}
				});
			}
			
			Object_OnChange();
		}
	});
}

function Object_OnChange()
{
	var object = $("[name=Object]");
	if (object.val() == "")
		return;

	SetStatusMessage(_lang_working);

	$.ajax(
	{
		url : "ajax_property.aspx?Namespace=" + $("[name=Module] :selected").attr("Namespace") + "&ObjectID=" + object.val(),
		data : {},
		type : "GET",
		error : function(request)
		{
			Alert(_lang_operation_fail);
			SetStatusMessage();
		},
		success : function(data)
		{
			SetStatusMessage();

			_properties = eval(data);
			Property_OnChange();
		}
	});
}

function Property_OnChange()
{
	$(".Table1 tbody").empty();
	
	if (_export == null)
		return;
	
	//字段映射
	var fields = _export.documentElement.selectNodes("Fields/Field");
	for (var i = 0; i < fields.length; i ++)
	{
		var tr = "<tr>";

		tr += "<td><select>";
		var from = fields[i].getAttribute("From");
		for (var j = 0; j < _fields.length; j++)
		{
			if (from == _fields[j])
				tr += "<option value='" + _fields[j] + "' selected='selected'>" + _fields[j] + "</option>";
			else
				tr += "<option value='" + _fields[j] + "'>" + _fields[j] + "</option>";
		}
		tr += "</select></td>";

		tr += "<td><select>";
		var to = fields[i].getAttribute("To");
		for (var j = 0; j < _properties.length; j++)
		{
			if (to == _properties[j])
				tr += "<option value='" + _properties[j] + "' selected='selected'>" + _properties[j] + "</option>";
			else
				tr += "<option value='" + _properties[j] + "'>" + _properties[j] + "</option>";
		}
		tr += "</select></td>";
		
		tr += "<td>";
		if (fields[i].getAttribute("Unique") == "1")
			tr += "<input type='checkbox' checked='checked' />";
		else
			tr += "<input type='checkbox' />";
		tr += "</td>";
		
		tr += "<td>";
		if (fields[i].getAttribute("MapOnly") == "1")
			tr += "<input type='checkbox' checked='checked' />";
		else
			tr += "<input type='checkbox' />";
		tr += "</td>";
		
		tr += "<td><button type='button'>" + _lang_delete + "</button></td>";

		tr += "</tr>";
		
		$(".Table1:first tbody").append(tr);
	}
	
	//停止条件
	var conditions = _export.documentElement.selectNodes("Stop/Condition");
	for (var i = 0; i < conditions.length; i ++)
	{
		var tr = "<tr>";
	
		tr += "<td><select>";
		var field = conditions[i].getAttribute("Field");
		for (var j = 0; j < _fields.length; j++)
		{
			if (field == _fields[j])
				tr += "<option value='" + _fields[j] + "' selected='selected'>" + _fields[j] + "</option>";
			else
				tr += "<option value='" + _fields[j] + "'>" + _fields[j] + "</option>";
		}
		tr += "</select></td>";
	
		tr += "<td><select>";
		var operation = conditions[i].getAttribute("Operation");
		for (var j = 0; j < _operations.length; j++)
		{
			if (operation == _operations[j])
				tr += "<option value='" + _operations[j] + "' selected='selected'>" + _operations[j] + "</option>";
			else
				tr += "<option value='" + _operations[j] + "'>" + _operations[j] + "</option>";
		}
		tr += "</select></td>";
		
		tr += "<td><input type='text' value='" + conditions[i].getAttribute("Value") + "' /></td>";
		tr += "<td><button type='button'>" + _lang_delete + "</button></td>";
		tr += "</tr>";
		
		$(".Table1:last tbody").append(tr);
	}
}

function SaveRuleExport()
{
	if (this.Module.value == "")
	{
		Alert(_lang_module_empty);
		return false;
	}	
	
	if (this.Object.value == "")
	{
		Alert(_lang_object_empty);
		return false;
	}
	
	var xml = "<Export Module=\"" + $("[name=Module] :selected").attr("Namespace") + "\" Instance=\"" + $("[name=Instance]").val() + "\" Object=\"" + $("[name=Object] :selected").text() + "\">";
	xml += "<Fields>";
	$(".Table1:first tbody tr").each(function()
	{
		xml += "<Field From=\"" + $(this).find("select:first").val() + "\" To=\"" + $(this).find("select:last").val() + "\" Unique=\"" + ($(this).find(":checkbox")[0].checked ? 1 : 0) + "\" MapOnly=\"" + ($(this).find(":checkbox")[1].checked ? 1 : 0) + "\" />";
	});
	xml += "</Fields>";
	xml += "<Stop>";
	$(".Table1:last tbody tr").each(function()
	{
		xml += "<Condition Field=\"" + $(this).find("select:first").val() + "\" Operation=\"" + $(this).find("select:last").val().replace(">", "&gt;").replace("<", "&lt;") + "\" Value=\"" + $(this).find(":text").val() + "\" />";
	});
	xml += "</Stop>";
	xml += "</Export>";
	
	var data = "Export=" + encodeURIComponent(xml);

	$(":submit")[0].disabled = true;
	SetStatusMessage(_lang_working);
	
	$.ajax(
	{
		url : _webPath + "alicms.aspx?Lang=" + _lang + "&Object=Rule&Operation=SetExport&ID=" + _id,
		data : data,
		type : "POST",
		error : function(request)
		{
			Alert(_lang_operation_fail);
			SetStatusMessage();
			$(":submit")[0].disabled = false;
		},
		success : function(data)
		{
			Alert(_lang_operation_success);
			SetStatusMessage();
			$(":submit")[0].disabled = false;
		}
	});
	
	return false;	
}

function AddFieldMap()
{
	if ($("[name=Object]").val() == "")
		return;

	var tr = "<tr>";

	tr += "<td><select>";
	for (var j = 0; j < _fields.length; j++)
	{
		tr += "<option value='" + _fields[j] + "'>" + _fields[j] + "</option>";
	}
	tr += "</select></td>";

	tr += "<td><select>";
	for (var j = 0; j < _properties.length; j++)
	{
		tr += "<option value='" + _properties[j] + "'>" + _properties[j] + "</option>";
	}
	tr += "</select></td>";
	
	tr += "<td><input type='checkbox' /></td>";
	tr += "<td><input type='checkbox' /></td>";
	tr += "<td><button type='button'>" + _lang_delete + "</button></td>";
	tr += "</tr>";
	
	$(".Table1:first tbody").append(tr);
}

function AddStopCondition()
{
	if ($("[name=Object]").val() == "")
		return;

	var tr = "<tr>";

	tr += "<td><select>";
	for (var j = 0; j < _fields.length; j++)
	{
		tr += "<option value='" + _fields[j] + "'>" + _fields[j] + "</option>";
	}
	tr += "</select></td>";

	tr += "<td><select>";
	for (var j = 0; j < _operations.length; j++)
	{
		tr += "<option value='" + _operations[j] + "'>" + _operations[j] + "</option>";
	}
	tr += "</select></td>";
	
	tr += "<td><input type='text' /></td>";
	tr += "<td><button type='button'>" + _lang_delete + "</button></td>";
	tr += "</tr>";
	
	$(".Table1:last tbody").append(tr);
}

function DeleteRow()
{
	$(this).closest("tr").remove();
}
//End : 规则导出
//-------------------------------------------------

//-------------------------------------------------
//Start : 栏目设置
function SetBasicSettings()
{
	if (!this.Interval.value.match(/^\d*$/))
	{
		Alert(_lang_interval_incorrect, function(){this.Interval.select();}, this);
		return false;
	}	
	
	var data = "Interval=" + this.Interval.value;
	data += "&ConnectionString=" + this.ConnectionString.value;
	if ($("#EnableCheckYes")[0].checked)
		data += "&CheckTime=" + FormatDate(new Date($("[name=Year]").val(), $("[name=Month]").val(), $("[name=Date]").val(), $("[name=Hour]").val(), $("[name=Minute]").val()), "YYYY-MM-DD hh:mm:ss");
	else
		data += "&CheckTime=";

	$(":submit")[0].disabled = true;
	SetStatusMessage(_lang_working);
	
	$.ajax(
	{
		url : _webPath + "alicms.aspx?Lang=" + _lang + "&Object=InstanceConfig&Operation=SetBasicSettings",
		data : data,
		type : "POST",
		error : function(request)
		{
			Alert(_lang_operation_fail);
			SetStatusMessage();
			$(":submit")[0].disabled = false;
		},
		success : function(data)
		{
			Alert(_lang_operation_success);
			SetStatusMessage();
			$(":submit")[0].disabled = false;
		}
	});
	
	return false;	
}
//End : 栏目设置
//-------------------------------------------------

function Init()
{
	if (typeof (_page) == "undefined")
		return;
	
	if(_page == "rule_list")
	{
		MakeButton();
		
		$("#IsRecycled,#Sort").change(Search);
		$("#Keyword").keydown(function(e){if(e.keyCode == 13){Search(1);e.preventDefault();}});
		$("#Search").click(function(){Search(1);});
		$("#SelectAll").click(function(){$(":checkbox[name=Sel]").each(function(){this.checked = true;});});
		$("#ReverseSelect").click(function(){$(":checkbox[name=Sel]").each(function(){this.checked = !this.checked;});});
	}
	else if(_page == "rule_add")
	{
		//月份改变时，调整日期
		$("[name=Month]").change(function()
		{
			$("[name=Date] [value=1]").attr("selected", true);
			var lastDate = new Date($("[name=Year]").val(), this.value, 0);
			
			var last = lastDate.getDate();
			$("[name=Date] option").each(function()
			{
				if (parseInt(this.value) > last)
					this.disabled = true;
				else
					this.disabled = false;
			});
		});

		//开始时间
		var now = new Date();
		$("[name=Month]").val(now.getMonth());
		$("[name=Date]").val(now.getDate());
		$("[name=Hour]").val(now.getHours());
		$("[name=Minute]").val(now.getMinutes());
		
		$("form:first").submit(AddRule);
		
		$("[name=Every]").change(function()
		{
			if (this.value == "")
				$("#StartTimeTR").hide();
			else
				$("#StartTimeTR").show();
			
			if (this.value == "Week")
				$("#Day").show();
			else
				$("#Day").hide();
		});
		
		$("[name=Year]").blur(function()
		{
			if (!this.value.match(/^\d{4}$/) || parseInt(this.value) < now.getFullYear())
				this.value = now.getFullYear();
		});
		
		$("#GuidCheckBox").click(function()
		{
			this.form.ID.disabled = !this.checked;
			if (this.form.ID.disabled)
				this.form.ID.className = "Disabled";
			else
				this.form.ID.className = "Text";
		});
	}
	else if(_page == "rule_edit")
	{
		//月份改变时，调整日期
		$("[name=Month]").change(function()
		{
			$("[name=Date] [value=1]").attr("selected", true);
			var lastDate = new Date($("[name=Year]").val(), this.value, 0);
			
			var last = lastDate.getDate();
			$("[name=Date] option").each(function()
			{
				if (parseInt(this.value) > last)
					this.disabled = true;
				else
					this.disabled = false;
			});
		});
		
		//发送方式
		$("[name=Every]").val(_every);

		//开始时间
		var startTime = new Date(_startTime);
		$("[name=Year]").val(startTime.getFullYear());
		$("[name=Month]").val(startTime.getMonth());
		$("[name=Date]").val(startTime.getDate());
		$("[name=Hour]").val(startTime.getHours());
		$("[name=Minute]").val(startTime.getMinutes());
		if (_every != "")
			$("#StartTimeTR").show();
		
		$("form:first").submit(EditRule);
		$("#Now").click(function(){$("[name=EndTime]").val(FormatDate(new Date(), "YYYY-MM-DD hh:mm:ss"));return false;});
		
		$("[name=Every]").change(function()
		{
			if (this.value == "")
				$("#StartTimeTR").hide();
			else
				$("#StartTimeTR").show();
			
			if (this.value == "Week")
				$("#Day").show();
			else
				$("#Day").hide();
		});
		
		$("[name=Year]").blur(function()
		{
			if (!this.value.match(/^\d{4}$/) || parseInt(this.value) < now.getFullYear())
				this.value = now.getFullYear();
		});
		
		$("#GuidCheckBox").click(function()
		{
			this.form.ID.disabled = !this.checked;
			if (this.form.ID.disabled)
				this.form.ID.className = "Disabled";
			else
				this.form.ID.className = "Text";
		});
	}
	else if (_page == "rule_export")
	{
		if ($.trim(_export) == "")
		{
			_export = null;
		}
		else
		{
			var temp = unescape(_export);
			_export = CreateXmlDom();
			_export.loadXML(temp);
			
			var module = _export.documentElement.getAttribute("Module");
			if (module != null)
				$("[Namespace='" + module + "']").attr("selected", true);
		}
		
		$("form:first").submit(SaveRuleExport);
		$("[name=Module]").change(Module_OnChange).change();
		$("[name=Object]").change(Object_OnChange);
		$(".Table1:first tfoot button").click(AddFieldMap);
		$(".Table1:last tfoot button").click(AddStopCondition);
		$(".Table1 tbody").delegate("td:last-child button", "click", DeleteRow);
	}
	else if(_page == "result_list")
	{
		if (_isRunning == "True")
		{
			$("#StartExecute")[0].disabled = true;
			SetStatusMessage(_lang_start_working);
			setTimeout(RefreshResult, _interval);
		}
		
		$("#Sort").change(Search);
		$("#Keyword").keydown(function(e){if(e.keyCode == 13){Search(1);e.preventDefault();}});
		$("#Search").click(function(){Search(1);});
		$("#SelectAll").click(function(){$(":checkbox[name=Sel]").each(function(){this.checked = true;});});
		$("#ReverseSelect").click(function(){$(":checkbox[name=Sel]").each(function(){this.checked = !this.checked;});});
		$("#StartExecute").click(StartExecute);
		$("#Export").click(Export);
		$(document.body).delegate("#StopExecute", "click", StopExecute);
	}
	else if (_page == "result_replace")
	{
		$("form").submit(ReplaceResult);
	}
	else if (_page == "config_basic")
	{
		//月份改变时，调整日期
		$("[name=Month]").change(function()
		{
			$("[name=Date] [value=1]").attr("selected", true);
			var lastDate = new Date($("[name=Year]").val(), this.value, 0);
			
			var last = lastDate.getDate();
			$("[name=Date] option").each(function()
			{
				if (parseInt(this.value) > last)
					this.disabled = true;
				else
					this.disabled = false;
			});
		});
		
		$("[name=Year]").blur(function()
		{
			var now = new Date();
			if (!this.value.match(/^\d{4}$/) || parseInt(this.value) < now.getFullYear())
				this.value = now.getFullYear();
		});

		//开始时间
		var checkTime = new Date(_checkTime);
		$("[name=Year]").val(checkTime.getFullYear());
		$("[name=Month]").val(checkTime.getMonth());
		$("[name=Date]").val(checkTime.getDate());
		$("[name=Hour]").val(checkTime.getHours());
		$("[name=Minute]").val(checkTime.getMinutes());
		if ($("#EnableCheckYes")[0].checked)
			$("#CheckTime").show();

		$("form:first").submit(SetBasicSettings);
		
		$("[name=EnableCheck]").click(function()
		{
			if (this.id == "EnableCheckYes")
				$("#CheckTime").show();
			else
				$("#CheckTime").hide();
		});
	}
}

Init();